/**
 * 
 */
package com.wubo.sec.web.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.common.date.DateUtil;
import com.wubo.common.util.StringUtils;
import com.wubo.sec.model.ErrorLog;
import com.wubo.sec.service.IErrorLogService;

import flexjson.JSONSerializer;

/**
 * @author wubo
 * @CreateDate 2010-8-9
 * @version 1.0.01
 */
@Controller
public class ErrorLogMgtAction extends BaseSecAction {
	@Autowired
	private IErrorLogService errorLogService;
	
	public IErrorLogService getErrorLogService() {
		return errorLogService;
	}

	public void setErrorLogService(IErrorLogService errorLogService) {
		this.errorLogService = errorLogService;
	}
	
	private String type;
	
	private String orderby;
	
	private String ids;
	
	private String startDate;
	
	private String endDate;
	
	private List<ErrorLog> errors;
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getOrderby() {
		return orderby;
	}

	public void setOrderby(String orderby) {
		this.orderby = orderby;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public List<ErrorLog> getErrors() {
		return errors;
	}

	public void setErrors(List<ErrorLog> errors) {
		this.errors = errors;
	}

	public String list() throws Exception {
		StringBuilder where = new StringBuilder("where 1=1");
		if(StringUtils.hasText(type)){
			where.append( " and l.type like '%"+type+"%'" );
		}
		List<Date> params = new ArrayList<Date>(2);
		if(StringUtils.hasText(startDate)){
			params.add(DateUtil.StringToDate(startDate));
			where.append(" and l.occurTime >= ?");
		}
		if(StringUtils.hasText(endDate)){
			params.add(DateUtil.StringToDate(endDate+" 23:59:59"));
			where.append(" and l.occurTime <= ?");
		}
		if(! StringUtils.hasText(orderby)){
			orderby = " order by l.occurTime desc";
		}

		Object[] args = params.toArray();
		
		total= errorLogService.getTotal(where.toString(), args);
		
		errors = errorLogService.getSplitPage(start, limit>0?limit:20, where.toString()+orderby, args);
		return SUCCESS;
	}

	private String q; //用于AutoComplete
	public String getQ() {
		return q;
	}
	public void setQ(String q) {
		this.q = q;
	}
	public void autoComplete() {
		try{
			List<String> names = errorLogService.listType(q);
			json(new JSONSerializer().serialize(names));
		}catch(Throwable t){
			logger.error(t.getMessage(), t);
			jsonError(null);
		}
	}
	
	public String del() throws Exception {
		if(StringUtils.hasText(ids))
			errorLogService.del(ids);
		else
			errorLogService.delAll();
		
		list();
		
		flag = true;
		return SUCCESS;
	}
}
